{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# K近邻算法（KNN）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 9],\n",
       "       [6, 1],\n",
       "       [1, 2],\n",
       "       [8, 7],\n",
       "       [3, 5]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(1)\n",
    "X1 = np.random.randint(1, 10, size=10).reshape(-1, 2)\n",
    "X1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[15, 18],\n",
       "       [19, 15],\n",
       "       [10, 10],\n",
       "       [11, 17],\n",
       "       [16, 19]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(1)\n",
    "X2 = np.random.randint(10, 20, size=10).reshape(-1, 2)\n",
    "X2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 6,  9],\n",
       "       [ 6,  1],\n",
       "       [ 1,  2],\n",
       "       [ 8,  7],\n",
       "       [ 3,  5],\n",
       "       [15, 18],\n",
       "       [19, 15],\n",
       "       [10, 10],\n",
       "       [11, 17],\n",
       "       [16, 19]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train = np.concatenate([X1, X2])\n",
    "X_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train =np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])\n",
    "y_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYYAAAD8CAYAAABzTgP2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAFS1JREFUeJzt3X2sXHWdx/HPp1D+uNiUsr1bS+Heq4aQ6HZBdlJ1RYIWa2l4UGNcyESrkowYSCTZjcvuTfApN9E1usSHSEYg4GYWidEqrEXoNiZoIshtU2h50BbSW9ot7cWaW801kep3/5hzZX5zZ+7TPE/fr2Qy5/zO75zz7bmH+XDmPIwjQgAAzFjW6QIAAN2FYAAAJAgGAECCYAAAJAgGAECCYAAAJOYNBtsX2P6Z7WdtP2P701n7ubZ32N6fva+qM//WrM9+21ub/Q8AADSX57uPwfZaSWsjYrftFZJ2SXq/pI9JOhERX7J9m6RVEfGvVfOeK2lcUk5SZPP+Q0T8run/EgBAU8x7xBARRyNidzb8e0nPSVon6TpJ92Xd7lM5LKq9T9KOiDiRhcEOSZubUTgAoDXOXExn2yOS3irpCUlrIuJoNullSWtqzLJO0ksV44eztjmtXr06RkZGFlMaAJzWdu3a9UpEDDZjWQsOBtuvk/QDSbdGxEnbf50WEWG7oWdr2C5IKkjS0NCQxsfHG1kcAJxWbE80a1kLuirJ9nKVQ6EUET/Mmo9l5x9mzkMcrzHrEUkXVIyfn7XNEhHFiMhFRG5wsCmhBwBYgoVclWRJd0t6LiK+VjHpQUkzVxltlfTjGrM/ImmT7VXZVUubsjYAQJdayBHDOyV9RNJ7bO/JXlskfUnSe23vl3RlNi7bOdt3SVJE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H+1vMxNjUTxCwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X_train[y_train==0, 0], X_train[y_train==0, 1], color='r')\n",
    "plt.scatter(X_train[y_train==1, 0], X_train[y_train==1, 1], color='g')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([9, 10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYYAAAD8CAYAAABzTgP2AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvhp/UCwAAFU5JREFUeJzt3X2sXHWdx/HPp1D+uNiUsr1bS+HeUUNIdLsgO6m6IkGLtTQ8qDEuZKJVSUYMJJLsxmX3JviUm+gaXeJDJCMQcDOLxGgV1iJ0GxM0EeS2KbQ8aAvpLe2W9mLNreaaSPW7f8y5Mr+5M/dpnm/fr2Qy5/zO75zz7bmH+XDmPIwjQgAATFvW7QIAAL2FYAAAJAgGAECCYAAAJAgGAECCYAAAJOYMBtsX2P6Z7WdtP2P701n7ubZ32N6fva9qMP/WrM9+21tb/Q8AALSW57qPwfZaSWsjYrftFZJ2SXq/pI9JOhERX7J9m6RVEfGvNfOeK2lMUl5SZPP+Q0T8ruX/EgBAS8x5xBARRyNidzb8e0nPSVon6TpJ92Xd7lMlLGq9T9KOiDiRhcEOSZtbUTgAoD3OXEhn2zlJb5X0hKQ1EXE0m/SypDV1Zlkn6aWq8cNZ26xWr14duVxuIaUBwGlt165dr0TEYCuWNe9gsP06ST+QdGtEnLT912kREbaberaG7aKkoiQ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VPS9qTvbZIuknSTVmfWyQ9o8qVE49L+scu1PnGbP1PZbWMZO3VdVrSt1S52mOvpHyXtunZqnzQr6xq64ntqUpYHZX0qirfwd4o6W8k7ZS0X9L/Sjo365uXdFfVvJ+QdCB7fbzDNR5Q5fvj6X30zqzveZK2z7aPdGF7/le2/z2tygfa2tpas/Etqlx180K7a61XZ9Z+7/R+WdW3a9t0ls+jtuyj3PkMAEjwVRIAIEEwAAASBAMAIEEwAAASBAMAIEEwAAASBAMAIEEwAAAS/w/XXOlleBWFNAAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X_train[y_train==0, 0], X_train[y_train==0, 1], color='r')\n",
    "plt.scatter(X_train[y_train==1, 0], X_train[y_train==1, 1], color='g')\n",
    "\n",
    "plt.scatter(x[0], x[1], color='b')\n",
    "\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "def distance(a, b, p=2):\n",
    "    return np.sum(np.abs(a - b) ** p) ** (1/p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3.1622776601683795,\n",
       " 9.486832980505138,\n",
       " 11.313708498984761,\n",
       " 3.1622776601683795,\n",
       " 7.810249675906654,\n",
       " 10.0,\n",
       " 11.180339887498949,\n",
       " 1.0,\n",
       " 7.280109889280518,\n",
       " 11.40175425099138]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "distances = [distance(x, item) for item in X_train]\n",
    "distances"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([7, 0, 3, 8, 4, 1, 5, 6, 2, 9])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ind = np.argsort(distances)\n",
    "ind"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[10, 10],\n",
       "       [ 6,  9],\n",
       "       [ 8,  7],\n",
       "       [11, 17],\n",
       "       [ 3,  5],\n",
       "       [ 6,  1],\n",
       "       [15, 18],\n",
       "       [19, 15],\n",
       "       [ 1,  2],\n",
       "       [16, 19]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train[ind]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "k = 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 0, 0])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train[ind][:k]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Counter({1: 1, 0: 2})"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from collections import Counter\n",
    "votes = Counter(y_train[ind][:k])\n",
    "votes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(0, 2), (1, 1)]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "votes.most_common(1)     # 1 代表最多的 1 个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "predict_y = votes.most_common(1)[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predict_y"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
